// pages/video/video.js
import utils from "../../utils/utils.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    id: "",
    imgUrl: "",
    focusId: [],
    focusNum: [],//关注中是否存在该用户ID
    upId: [],
    upNum: [],
    showComment: false,
    commentArr: [],
    comment:"",
    videoId: "",
    name: ""
  },

  /**
   * 点击弹出评论区
   */
  showComment(e) {
    // console.log(e);
    // console.log(this.data.imgUrl);
    // console.log(this.data.name);
    this.setData({ 
      showComment: true,
      videoId: e.target.dataset.id
    });
    // console.log(e.target.dataset.id);
    /**
     * 拿到评论数据
     */
    wx.cloud.callFunction({
      name: "getCommentData",
      data: {
        id: e.target.dataset.id
      }
    }).then(res=>{
      // console.log(res);
      // console.log(res.result.data);
      let arr = []
      res.result.data.forEach(item=>{
        item.time = utils.getMyDate(item.time,"Y-m-d H:i:s")
        arr.push(item)
      })
      this.setData({
        commentArr: arr
      })
    })
  },

  /**
   * 提交评论
   */
  upComment(){
    // console.log(this.data.comment);
    let obj = {}
    obj.id = this.data.id
    obj.context = this.data.comment
    obj.imgUrl = this.data.imgUrl
    obj.time = new Date().getTime()/1000
    obj.name = this.data.name
    obj.videoId = this.data.videoId
    wx.cloud.callFunction({
      name: "updateCommentData",
      data: {
        action: "add",
        obj
      }
    }).then(res=>{
      this.setData({
        comment: ""
      })
      this.closeComment()
      wx.switchTab({
        url: '/pages/own/own'
      })
    })

    /**
     * 更改评论数量
     */
    wx.cloud.callFunction({
      name: "updateCommentNumber",
      data: {
        id: this.data.videoId,
        action: "plusComment"
      }
    })
  },

  /**
   * 点击删除评论
   */
  deleteComment(e){
    // console.log(e.target.dataset);
    wx.showModal({
      content: '确认删除这条评论吗',
      success: res=> {
        if (res.confirm) {
          /**
           * 更改评论数量
           */
          wx.cloud.callFunction({
            name: "updateCommentNumber",
            data: {
              id: e.target.dataset.url,
              action: "minusComment"
            }
          })

          /**
           * 删除评论内容
           */
          wx.cloud.callFunction({
            name: "updateCommentData",
            data: {
              id: e.target.dataset.id,
              action: "delete"
            }
          }).then(res=>{
            this.closeComment()
            wx.switchTab({
              url: '/pages/own/own'
            })
          })
        }
      }
    })
  },

  /**
   * 点击关闭评论区
   */
  closeComment() {
    this.setData({ 
      showComment: false,
      commentArr: []
    });
  },

  /**
   * 点击关注
   */
  clickFocus(e){
    // console.log(e.target.dataset.id);
    // console.log(this.data.id)
    /**
     * 更改关注样式
     */
    wx.cloud.callFunction({
      name: "updateFocusId",
      data: {
        id: this.data.id,
        focusid: e.target.dataset.id,
        action: "plusId"
      }
    }).then(res=>{
      this.onLoad()
    })

    /**
     * 更改关注数量
     */
    wx.cloud.callFunction({
      name: "updateFocusNumber",
      data: {
        id: e.target.dataset.id,
        action: "plusFans"
      }
    })

    wx.cloud.callFunction({
      name: "updateFocusNumber",
      data: {
        id: this.data.id,
        action: "plusFocus"
      }
    })
  },

  /**
   * 点击取消关注
   */
  clickCancelFocus(e){
    // console.log(e.target.dataset.id);
    /**
     * 更改关注样式
     */
    wx.cloud.callFunction({
      name: "updateFocusId",
      data: {
        id: this.data.id,
        focusid: e.target.dataset.id,
        action: "minusId"
      }
    }).then(res=>{
      this.onLoad()
    })

    /**
     * 更改关注数量
     */
    wx.cloud.callFunction({
      name: "updateFocusNumber",
      data: {
        id: e.target.dataset.id,
        action: "minusFans"
      }
    })

    wx.cloud.callFunction({
      name: "updateFocusNumber",
      data: {
        id: this.data.id,
        action: "minusFocus"
      }
    })
  },

  /**
   * 点击点赞
   */
  clickUp(e){
    // console.log(e.target.dataset.url);
    /**
     * 更改点赞样式
     */
    wx.cloud.callFunction({
      name: "updateUpId",
      data: {
        id: this.data.id,
        upid: e.target.dataset.url,
        action: "plusId"
      }
    }).then(res=>{
      this.onLoad()
    })

    /**
     * 更改点赞数量
     */
    wx.cloud.callFunction({
      name: "updateUpNumber",
      data: {
        id: e.target.dataset.id,
        action: "plusUps"
      }
    })

    wx.cloud.callFunction({
      name: "updateUpNumber",
      data: {
        // id: e.target.dataset.id,
        url: e.target.dataset.url,
        action: "plusUp"
      }
    })
  },

  /**
   * 点击取消点赞
   */
  clickCancelUp(e){
    // console.log(e.target.dataset.url);
    /**
     * 更改点赞样式
     */
    wx.cloud.callFunction({
      name: "updateUpId",
      data: {
        id: this.data.id,
        upid: e.target.dataset.url,
        action: "minusId"
      }
    }).then(res=>{
      this.onLoad()
    })

    /**
     * 更改关注数量
     */
    wx.cloud.callFunction({
      name: "updateUpNumber",
      data: {
        id: e.target.dataset.id,
        action: "minusUps"
      }
    })

    wx.cloud.callFunction({
      name: "updateUpNumber",
      data: {
        // id: e.target.dataset.id,
        url: e.target.dataset.url,
        action: "minusUp"
      }
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '推荐'
    })

    /**
     * 获取视频数据，渲染到视频页
     */
    wx.cloud.callFunction({
      name: "getVideoData"
    }).then(res=>{
      let arr = []
      // console.log(res.result.data);
      res.result.data.forEach(item=>{
        // console.log(item);
        item.passtime = utils.getMyDate(item.passtime,"Y-m-d H:i:s")
        // console.log(item2.passtime);
        arr.push(item)
        // console.log(arr);
      })
      arr.sort((a,b)=>a['passtime'] > b['passtime'] ? -1 : 1)
      this.setData({
        dataList: arr,
        id: getApp().globalData.id
      })
    })

    /**
     * 获取该用户关注的用户ID
     */
    wx.cloud.callFunction({
      name: "getUser",
      data: {
        id: this.data.id
      }
    }).then(res=>{
      // console.log(res.result.data.focusId);
      this.setData({
        focusId: res.result.data.focusId,
        imgUrl: res.result.data.avatarUrl,
        name: res.result.data.nickName
      })
      let arr = []
      this.data.dataList.forEach(item=>{
        let idx = this.data.focusId.indexOf(item._id)
        // console.log(idx);
        arr.push(idx)
      })
      this.setData({
        focusNum: arr
      })
    })

    /**
     * 获取该用户点赞的视频url
     */
    wx.cloud.callFunction({
      name: "getUser",
      data: {
        id: this.data.id
      }
    }).then(res=>{
      // console.log(res);
      this.setData({
        upId: res.result.data.upId
      })
      let arr = []
      this.data.dataList.forEach(item=>{
        let idx = this.data.upId.indexOf(item.videoUrl)
        // console.log(idx);
        arr.push(idx)
      })
      this.setData({
        upNum: arr
      })
      // console.log(arr);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})